import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';
import 'package:edu/constants/config.dart';
import 'package:edu/networks/course/models/course_response.dart';

part 'course_api.g.dart';

abstract class CourseApi {
  // 获取课程分类
  Future<CourseCategoryResponse> getAllCategories();

  // 获取课程top
  Future<TopCourseKeysResponse> getTopCourseKeys();

  // 获取课程列表
  Future<CourseListResponse> getCourseList({
    int? categoryId,
    int? page,
    int? pageSize,
    CancelToken? cancelToken,
  });

  // 根据key的值查询课程
  Future<CourseListResponse> searchCoursesByKey({
    required String key,
    int? page,
    int? pageSize,
    CancelToken? cancelToken,
  });

  // 获取课程详情
  Future<CourseDetailResponse> getCourseDetail(int courseId);
}

@RestApi(baseUrl: Config.baseUrl)
abstract class RemoteCourseApi extends CourseApi {
  factory RemoteCourseApi(Dio dio, {String baseUrl}) = _RemoteCourseApi;

  /*
   * 获取课程分类
   */
  @GET('/v1/course/categories')
  @override
  Future<CourseCategoryResponse> getAllCategories();

  /*
   * 获取课程top
   */
  @GET('/v1/course/topCourseKeys')
  @override
  Future<TopCourseKeysResponse> getTopCourseKeys();

  /*
   * 获取课程列表
   */
  @GET('/v1/course/list')
  @override
  Future<CourseListResponse> getCourseList({
    @Query('category_id') int? categoryId,
    @Query('page') int? page,
    @Query('page_size') int? pageSize,
    @CancelRequest() CancelToken? cancelToken,
  });

  /*
   * 根据key的值查询课程
   */
  @GET('/v1/course/search')
  @override
  Future<CourseListResponse> searchCoursesByKey({
    @Query('key') required String key,
    @Query('page') int? page,
    @Query('page_size') int? pageSize,
    @CancelRequest() CancelToken? cancelToken,
  });

  /*
   * 获取课程详情
   */
  @GET('/v1/course/detail')
  @override
  Future<CourseDetailResponse> getCourseDetail(
    @Query('course_id') int courseId,
  );
}
